﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEViolationHSEService
{
    private readonly SimpleDbContext _context;
    private readonly CacheService _cacheService;
    public QHSEViolationHSEService(SimpleDbContext context,CacheService cacheService)
    {
        _context = context;
        _cacheService = cacheService;
    }
    public async Task<List<QHSEViolationHSEModel>> GetAsync()
    {
        string name = "1609";
        var result = await _cacheService.GetQHSEViolationHSEAsync(name);
        if (result.Count == 0)
        {
            var violationHSEs = await _context.Set<QHSESysViolationHSE>().AsNoTracking().ToListAsync();
            var models = MapperHelper.Map<List<QHSEViolationHSEModel>>(violationHSEs);
            await _cacheService.SetQHSEViolationHSEAsync(models, name);
            return models;
        }
        return result;
    }
    public async Task<List<QHSEViolationHSEModel>> GetAsync(SimpleDbContext context)
    {
        string name = "1609";
        var result = await _cacheService.GetQHSEViolationHSEAsync(name);
        if (result.Count == 0)
        {
            var violationHSEs = await context.Set<QHSESysViolationHSE>().AsNoTracking().ToListAsync();
            var models = MapperHelper.Map<List<QHSEViolationHSEModel>>(violationHSEs);
            await _cacheService.SetQHSEViolationHSEAsync(models, name);
            return models;
        }
        return result;
    }
    public async Task<string> GetCodeByItemAsync(string item)
    {
        var id = await _context.Set<QHSESysViolationHSE>().AsNoTracking().Where(u => u.Item.Equals(item)).Select(u => u.Code).FirstOrDefaultAsync();
        return id;
    }
    public async Task<PageResultModel<QHSEViolationHSEModel>> GetPageAsync(QHSEViolationHSEPageInputModel input)
    {
        var result = new PageResultModel<QHSEViolationHSEModel>();
        var query = _context.Set<QHSESysViolationHSE>().AsNoTracking().AsQueryable();

        if (!string.IsNullOrEmpty(input.Code))
        {
            query = query.Where(u => EF.Functions.Like(u.Code, $"%{input.Code}%"));

        }


        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        query = query.OrderBy(u => u.Code).Page(input.PageNo, input.PageSize);
        var violationHSEs = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSEViolationHSEModel>>(violationHSEs);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

    public async Task<int> AddAsync(QHSEViolationHSEModel model)
    {
        var violationHSE= MapperHelper.Map<QHSESysViolationHSE>(model);
        await _context.AddAsync(violationHSE);
        return await _context.SaveChangesAsync();
    }


    public async Task<int> UpdateAsync(QHSEViolationHSEModel model)
    {

        var violationHSE = await _context.Set<QHSESysViolationHSE>()
            .Where(a => model.Id == a.Id)
            .FirstOrDefaultAsync();

        if (violationHSE == null)
        {
            throw AppResultException.Status404NotFound("找不到记录，更新失败");
        }

        MapperHelper.Map<QHSEViolationHSEModel, QHSESysViolationHSE>(model, violationHSE);
        _context.Update(violationHSE);
        int ret = await _context.SaveChangesAsync();

        if (ret == 0)
        {
            throw AppResultException.Status200OK("更新记录数为0");
        }

        return ret;
    }

    public async Task<int> DeleteAsync(IEnumerable<string> ids)
    {
        var violationHSEs = await _context.Set<QHSESysViolationHSE>()
            .Where(a => ids.Contains(a.Id))
            .ToListAsync();

        _context.RemoveRange(violationHSEs);
        int ret = await _context.SaveChangesAsync();
        return ret;
    }
}
